package com.jams.goodox.activity.serve.enterprise;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.entity.OrderTypeInfo;
import com.jams.goodox.entity.resp.PartyWorkResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.TypePopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jams.goodox.R.id.mPager;

/**
 * 企业政策
 */
public class EnterprisePolicyActivity extends BaseActivity {

    @Bind(R.id.lv_load)
    LoadView emptyView;
    @Bind(R.id.tabs)
    TabLayout enterprisePolicyTabLayout;
    @Bind(mPager)
    ViewPager enterprisePolicyPager;
    @Bind(R.id.iv_select)
    ImageView ivSelect;
    @Bind(R.id.ll_tabs)
    LinearLayout llTabs;
    /**
     * 企业政策标题
     */
    List<String> tittleList = new ArrayList<>();
    /**
     * 显示的Fragment 集合
     */
    private List<Fragment> contentList = new ArrayList<>();
    private FragmentPagerAdapter adapter;

    /**
     * popwindow的显示状态
     */
    private boolean isShow = false;
    /**
     * 显示popwindow的数据
     */
    private List<OrderTypeInfo> typeInfos = new ArrayList<>();
    /**
     * 选择的item的条目
     */
    private int selectPosition = 0;
    @Override

    protected int getLayoutId() {
        return R.layout.activity_personnel_policy;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "企业政策");
        initView();
        getData();


    }



    @OnClick(R.id.iv_select)
    public void onViewClicked() {

            //显示popwindow
        if (isShow) {
            isShow = false;
            ivSelect.setImageResource(R.mipmap.pop_down);
            TypePopupWindow.getInstance().dismissPop();

            //隐藏popwindow
        } else {
            isShow = true;
            ivSelect.setImageResource(R.mipmap.icon_menu_close);
            TypePopupWindow.getInstance().showTabTypePopupWindow(enterprisePolicyTabLayout, mContext, typeInfos, new TypePopupWindow.DataChangeListener() {
                @Override
                public void change(int position) {
                    selectPosition = position;
                    enterprisePolicyPager.setCurrentItem(position);
                }

                @Override
                public void dismiss() {
                    isShow = false;
                    ivSelect.setImageResource(R.mipmap.pop_down);

                }
            }, selectPosition);
        }


    }

    /**
     * 获取数据
     */
    private void getData() {
        showLoading();
        enqueue(createCallService(EnterpriseService.class).getPolicyType(), new RetrofitCallbackListener<PartyWorkResp>() {
            @Override
            public void onSuccess(Call<PartyWorkResp> call, PartyWorkResp result) {
                misLoading();
                try {
                    if (result.data == null) {
                        emptyView.setVisibility(View.VISIBLE);
                       // emptyView.loadCompleteNoData(R.mipmap.holder_no_common_cj,R.string.no_content);
                        return;
                    }
                    tittleList.clear();
                    contentList.clear();
                    for (PartyWorkResp.PartyWorkData data : result.data) {
                        tittleList.add(StringUtils.newString(data.name));
                        contentList.add(EnterprisePolicyFragment.newInstance(StringUtils.newString(data.id)));
                    }
                    if (tittleList != null && contentList != null) {
                        emptyView.setVisibility(View.GONE);
                        llTabs.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                        enterprisePolicyPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                selectPosition = position;
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                    }
                    typeInfos.clear();
                    OrderTypeInfo info;
                    for (String typeStr : tittleList) {
                        info = new OrderTypeInfo();
                        info.setTypeName(StringUtils.newString(typeStr));
                        typeInfos.add(info);
                    }


                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
                emptyView.setVisibility(View.VISIBLE);

            }
        });
    }

    /**
     * 初始化view
     */
    private void initView(){
        if (tittleList != null && contentList != null) {
            adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), tittleList, contentList);
            enterprisePolicyPager.setAdapter(adapter);
            enterprisePolicyTabLayout.setupWithViewPager(enterprisePolicyPager);
            emptyView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
        }
    }


}
